package com.topvision.jack.uploadlibrary.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

import com.topvision.jack.uploadlibrary.admin.TvUploadSdk;
import com.topvision.jack.uploadlibrary.task.UploadTaskListener;
import com.topvision.jack.uploadlibrary.utils.UploadUtils;

/**
 * Created by gushenjie on 17/12/24.
 */

public class UploadReceiver extends BroadcastReceiver {
    String baseApiUrl;
    UploadTaskListener uploadTaskListener;

    public UploadReceiver(String baseApiUrl, UploadTaskListener uploadTaskListener) {
        this.baseApiUrl = baseApiUrl;
        this.uploadTaskListener = uploadTaskListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //网络变化
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
            // 通过ping一个地址来判断是否能上网
            UploadUtils.ping(context);
        }
        //自定义广播事件。能连上外网的意思
        else if (UploadUtils.ACTION_NETWORK.equals(action)) {
            // callBack.checkUploadThread();
            TvUploadSdk.getInstance(context).uploadTasks(baseApiUrl, uploadTaskListener);
        }
        //无网络暂停所有上传进程
        else if (UploadUtils.ACTION_NETWORK_FAILED.equals(action)) {
            TvUploadSdk.getInstance(context).pauseTasks();
        }
    }
}
